// ==UserScript==
// @name        LoL Data Cache Creator
// @namespace   LoLTHDC
// @description LoL Data Cache Creator for Tribunal Helper
// @version     1.0
// @grant none
// @include     http://gameinfo.na.leagueoflegends.com/en/game-info/champions/
// @include     http://gameinfo.na.leagueoflegends.com/en/game-info/items/
// ==/UserScript==

	
var THData = (function () {
	
	itemCache = {};
	championCache = {};
	summonerCache = {};
	
	function fetchItems() {
		return Riot.DDragon.useModel('item').data;
	}
	
	function fetchChampions() {
		return Riot.DDragon.useModel('champion').data;
	}
	
	function fetchSummoners() {
		return Riot.DDragon.useModel('summoner').data;
	}
	
	function fetchAll() {
		var itemLoad = Riot.DDragon.useModel('item').data,
		championLoad = Riot.DDragon.useModel('champion').data,
		summonerLoad = Riot.DDragon.useModel('summoner').data,
		masteryLoad = Riot.DDragon.useModel('mastery').data,
		runeLoad = Riot.DDragon.useModel('rune').data;
	}
	
	function parseItems(data) {
		//console.log(data);
		
		$.each(data, function(key,val){
			var sprite = data[key].image.sprite
			var spriteNumber = sprite[sprite.length-5]
			var x = data[key].image.x;
				if ( x !== 0 ) {
					x = -x;
				};
			var y = data[key].image.y;
				if ( y !== 0 ) {
					y = -y;
				};
			//console.log("x: "+x+" y: "+y+" key: "+key);
			if ( itemCache[spriteNumber] === undefined ) {
			    itemCache[spriteNumber] = {};
			}
			if ( itemCache[spriteNumber] [x] === undefined ) {
			    itemCache[spriteNumber] [x] = {};
			}
			if ( itemCache[spriteNumber] [x][y] === undefined ) {
			    itemCache[spriteNumber] [x][y] = {};
			}
			itemCache[ spriteNumber ] [ x ][ y ] = key;
		});
		
		itemCache["patch"] =  Riot.DDragon.useModel('item').version;

		return itemCache;
	}
	
	function parseChampions(data) {
		//console.log(data);
		
		$.each(data, function(key,val){
			var sprite = data[key].image.sprite
			var spriteNumber = sprite[sprite.length-5]
			var x = data[key].image.x;
				if ( x !== 0 ) {
					x = -x;
				};
			var y = data[key].image.y;
				if ( y !== 0 ) {
					y = -y;
				};
			//console.log("x: "+x+" y: "+y+" key: "+key);
			if ( championCache[spriteNumber] === undefined ) {
			    championCache[spriteNumber] = {};
			}
			if ( championCache[spriteNumber] [x] === undefined ) {
			    championCache[spriteNumber] [x] = {};
			}
			if ( championCache[spriteNumber] [x][y] === undefined ) {
			    championCache[spriteNumber] [x][y] = {};
			}
			championCache[ spriteNumber ] [ x ][ y ] = key;
		});
		
		championCache["patch"] =  Riot.DDragon.useModel('champion').version;
		
		return championCache;
	}
	
	function parseSummoners(data) {
		//console.log(data);
		
		$.each(data, function(key,val){
			var x = data[key].image.x;
				if ( x !== 0 ) {
					x = -x;
				};
			var y = data[key].image.y;
				if ( y !== 0 ) {
					y = -y;
				};
			//console.log("x: "+x+" y: "+y+" key: "+key);
			if ( summonerCache[x] === undefined ) {
			    summonerCache[x] = {};
			}
			if ( summonerCache[x][y] === undefined ) {
			    summonerCache[x][y] = {};
			}
			summonerCache[ x ][ y ] = key;
		});
		
		summonerCache["patch"] =  Riot.DDragon.useModel('summoner').version;
		
		return summonerCache;
	}
	
    return {
        run: function () {
			console.log("FETCHING ALL DATA DRAGON MODELS");
			fetchAll();
			setTimeout( function() {
				console.log("RUNNING TRIBUNAL DATA CACHE!!!");
				console.log("ITEM CACHE:");
				console.log(JSON.stringify(parseItems(fetchItems())));
				console.log("CHAMPION CACHE:");
				console.log(JSON.stringify(parseChampions(fetchChampions())));
				console.log("SUMMONER CACHE:");
				console.log(JSON.stringify(parseSummoners(fetchSummoners())));
				console.log("TRIBUNAL DATA CACHE COMPLETED!!!");
			}, 5000);
        }
    };
}());

window.addEventListener('load', function () {
	setTimeout(function() {
		THData.run();
	}, 1000);
}, false);
